package com.weigee.ktv.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.l.d.app.library.view.noscroll.NoScrollGridView;
import com.weigee.ktv.R;
import com.weigee.ktv.v.adapter.grid.SingingTimeListAdapter;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;

/**
 * Created by linde on 2017/4/18.
 * <p/>
 * 选择欢唱时间
 */
public class CheckSingingTimeFragment extends BaseFragment implements View.OnTouchListener {
    @BindView(R.id.scroll_view)
    ScrollView mSv;

    @BindView(R.id.grid_morning_time)
    NoScrollGridView m_gridMorning;// 上午时间列表
    @BindView(R.id.grid_afternoon_time)
    NoScrollGridView m_gridAfternoon;// 下午时间列表
    @BindView(R.id.grid_next_morning_time)
    NoScrollGridView m_gridNextMorning;// 第二天上午时间列表

    private SingingTimeListAdapter mMorningAdapter;
    private SingingTimeListAdapter mAfternoonAdapter;
    private SingingTimeListAdapter mNextMorningAdapter;

    private Calendar mCalendar;// 当前日期展示
    private Calendar mCalendarStart;// 欢唱开始时间（未选为空）
    private Calendar mCalendarEnd;// 欢唱结束时间（未选为空）

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.checksingingtime_frag, container, false);
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        mSv.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mCalendar = Calendar.getInstance();

        mMorningAdapter = new SingingTimeListAdapter();
        mMorningAdapter.add(morningTimeList());
        m_gridMorning.setAdapter(mMorningAdapter);

        mAfternoonAdapter = new SingingTimeListAdapter();
        mAfternoonAdapter.add(afternoonTimeList());
        m_gridAfternoon.setAdapter(mAfternoonAdapter);

        mNextMorningAdapter = new SingingTimeListAdapter();
        mNextMorningAdapter.add(nextMorningTimeList());
        m_gridNextMorning.setAdapter(mNextMorningAdapter);
    }

    private ArrayList<Calendar> morningTimeList() {
        Calendar cal;
        final int year = mCalendar.get(Calendar.YEAR);
        final int month = mCalendar.get(Calendar.MONTH);
        final int day = mCalendar.get(Calendar.DATE);

        ArrayList<Calendar> list = new ArrayList<>();
        for (int hour = 0; hour < 12; hour++) {
            cal = Calendar.getInstance();
            cal.set(year, month, day, hour, 0, 0);
            list.add(cal);

            cal = Calendar.getInstance();
            cal.set(year, month, day, hour, 30, 0);
            list.add(cal);
        }
        return list;
    }

    private ArrayList<Calendar> afternoonTimeList() {
        Calendar calendar;
        final int year = mCalendar.get(Calendar.YEAR);
        final int month = mCalendar.get(Calendar.MONTH);
        final int day = mCalendar.get(Calendar.DATE);

        ArrayList<Calendar> list = new ArrayList<>();
        for (int hour = 12; hour < 24; hour++) {
            calendar = Calendar.getInstance();
            calendar.set(year, month, day, hour, 0, 0);
            list.add(calendar);

            calendar = Calendar.getInstance();
            calendar.set(year, month, day, hour, 30, 0);
            list.add(calendar);
        }
        return list;
    }

    private ArrayList<Calendar> nextMorningTimeList() {
        Calendar cal;
        final int year = mCalendar.get(Calendar.YEAR);
        final int month = mCalendar.get(Calendar.MONTH);
        final int day = mCalendar.get(Calendar.DATE) + 1;

        ArrayList<Calendar> list = new ArrayList<>();
        for (int hour = 0; hour <= 12; hour++) {
            cal = Calendar.getInstance();
            cal.set(year, month, day, hour, 0, 0);
            list.add(cal);

            cal = Calendar.getInstance();
            cal.set(year, month, day, hour, 30, 0);
            list.add(cal);
        }
        return list;
    }
}
